package plutuscat.shishicai.util;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 *
 *@author Jack
 *2016年5月8日下午9:43:34
 */
public class MyDateUtil extends DateUtils {

	public static int diffDay(String d1, String d2, String pattern) {
		try {
			Date date1 = DateUtils.parseDate(d1, pattern);
			Date date2 = DateUtils.parseDate(d2, pattern);
			return diffDay(date1, date2);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 相差天数的绝对值,只计算日期差,忽略时分秒
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int diffDay(Date d1, Date d2) {
		try {
			//格式化为yyyy-MM-dd 00:00:00
			long diffTimestamp = getOneDayMinTimestamp(d1) - getOneDayMinTimestamp(d2);
			long diff = diffTimestamp / (24 * 60 * 60 * 1000);
			return Math.abs(Integer.parseInt(diff + ""));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

	}

	public static long getOneDayMinTimestamp(Date d) throws ParseException {
		String date = DateFormatUtils.format(d, "yyyy-MM-dd").concat(" 00:00:00");
		return DateUtils.parseDate(date, "yyyy-MM-dd HH:mm:ss").getTime();
	}

	public static void main(String[] args) {
		int d = diffDay("2016-05-05 12:00:00", "2016-05-07 10:00:00", "yyyy-MM-dd HH:mm:ss");
		System.err.println(d);
	}
}
